package com.walle.userservice.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.walle.common.dto.MailDto;
import com.walle.common.entity.UserInfo;
import com.walle.common.exceptions.SystemException;
import com.walle.userservice.dao.UserInfoDao;
import com.walle.userservice.service.MailService;
import com.walle.userservice.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhangjiapeng
 * @Package com.walle.userservice.service.impl
 * @Description: ${todo}
 * @date 2018/12/26 16:12
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private MailService mailService;

    @Override
    public UserInfo getById(Long userId) {
        return userInfoDao.getById(userId);
    }

    @Override
    public UserInfo getByUserName(String username) {
        return userInfoDao.getByUserName(username);
    }

    @Override
    @HystrixCommand(fallbackMethod = "insertFall")
    public void insert(UserInfo userInfo) {
        userInfoDao.insert(userInfo);
        MailDto mailDto = new MailDto();
        mailDto.setUsername(userInfo.getUsername());
        mailDto.setTitle("注册邮件");
        mailDto.setContent("注册成功");
        mailService.sendMail(mailDto);
    }

    @HystrixCommand
    public void insertFall(UserInfo userInfo){
        throw new SystemException("注册失败");
    }
}
